package com.app.medicalcontrol.fragment.User.child;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.app.medicalcontrol.R;
import com.app.medicalcontrol.basis.BasisFragment;
import com.app.medicalcontrol.basis.Constants;
import com.app.medicalcontrol.bean.BasisBean;
import com.app.medicalcontrol.bean.LoginBean;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.OnClick;
import my.MD5;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

public class ResetPwdFragment extends BasisFragment{

    @BindView(R.id.et_old_pwd)
    EditText oldPwdEt;
    @BindView(R.id.et_new_pwd)
    EditText newPwdEt;
    @BindView(R.id.et_check_pwd)
    EditText checkPwdEt;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.fragment_reset_pwd);

        setTitle(getString(R.string.reset_pwd));

        setTitleRightButton(R.mipmap.top_icon_msg, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMessageInfo(v);
            }
        });
    }

    @OnClick(R.id.submit)
    public void resPwd(){

        showProgress();
        LoginBean.refreshToken(new LoginBean.Token_Listener() {
            @Override
            public void onSuccess() {
                showProgress();
                String oldPwd = oldPwdEt.getText().toString().trim();
                if(TextUtils.isEmpty(oldPwd)){
                    showToast("旧密码不可为空");
                    return;
                }

                String newPwd = newPwdEt.getText().toString().trim();
                if(TextUtils.isEmpty(newPwd)){
                    showToast("新密码不可为空");
                    return;
                }

                String checkPwd = checkPwdEt.getText().toString().trim();
                if(TextUtils.isEmpty(checkPwd)){
                    showToast("确认密码不可为空");
                    return;
                }
                RequestParams params = new RequestParams();
                params.put("ctoken", LoginBean.getInstance().getToken().getValue());
                params.put("old_password", MD5.getMD5ofStrLowercase(oldPwd));
                params.put("password",MD5.getMD5ofStrLowercase(newPwd));
                params.put("repassword",MD5.getMD5ofStrLowercase(checkPwd));
                HttpRestClient.post(Constants.URL_RESET_PWD, params, new MyHttpListener() {
                            @Override
                            public void onSuccess(int httpWhat, Object result) {
                                BasisBean bean = (BasisBean) result;
                                showToast(""+bean.getStatusInfo());
                                oldPwdEt.setText("");
                                newPwdEt.setText("");
                                checkPwdEt.setText("");
                            }

                            @Override
                            public void onFinish(int httpWhat) {
                                dismissProgress();
                            }

                            @Override
                            public void onFailure(int httpWhat, Object result) {
                                super.onFailure(httpWhat, result);
                            }
                        },
                        0, BasisBean.class);

            }

            @Override
            public void onFailure() {

            }

            @Override
            public void onFinish() {
                dismissProgress();
            }
        });



    }


}
